package ljl.codetop2;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class _179_largest_number {

    /**
     * 解出来了但是性能很差
     */
    static class a {
        public String largestNumber(int[] nums) {
            String[] arr = IntStream.of(nums).mapToObj(String::valueOf).toArray(String[]::new);
            Arrays.sort(arr, (a, b) -> Comparator.<String>reverseOrder().compare(a + b, b + a));
            String res = String.join("", arr);
            return new BigInteger(res).toString();
        }
    }

    /**
     * 它的解法比我的聪明的多一截子
     */
    static class off1 {
        public String largestNumber(int[] nums) {
            int n = nums.length;
            Integer[] numsArr = new Integer[n];
            for (int i = 0; i < n; i++) {
                numsArr[i] = nums[i];
            }

            // 他是在算 xy 和 yx 哪个大，要倒序，所以是 yx - xy
            Arrays.sort(numsArr, (x, y) -> {
                long sx = 10, sy = 10;
                while (sx <= x) {
                    sx *= 10;
                }
                while (sy <= y) {
                    sy *= 10;
                }
                return (int) (-sy * x - y + sx * y + x);
            });

            if (numsArr[0] == 0) {
                return "0";
            }
            StringBuilder ret = new StringBuilder();
            for (int num : numsArr) {
                ret.append(num);
            }
            return ret.toString();
        }
    }

    static class copy_off1 {
        public String largestNumber(int[] nums) {
            // 这如果用 stream 会慢一大截子，贼几把慢这鬼东西
            // 这是历史的倒退！
            int n = nums.length;
            Integer[] ints = new Integer[n];
            for (int i = 0; i < n; i++) {
                ints[i] = nums[i];
            }
            // ba - ab
            Arrays.sort(ints, (a, b) -> {
                long ma = 10, mb = ma;
                while (ma <= a) ma *= 10;
                while (mb <= b) mb *= 10;
                return (int) (ma * b + a - mb * a - b);
            });
            if (ints[0] == 0) return "0";
            StringBuilder sb = new StringBuilder(ints.length * 2);
            for (Integer i : ints) {
                sb.append(i);
            }
            return sb.toString();
        }
    }
}
